<template>
  <div class="app-container" v-loading="gcLoading">
    <h2 style="text-align: center">字体和七步法</h2>

    <el-steps
      :active="4"
      process-status="wait"
      align-center
      style="margin-bottom: 40px"
    >
      <el-step title="添加字体" />
      <el-step title="添加七步" />
    </el-steps>
    <div>
      <el-form :inline="true">
        <el-form-item>
          <el-select
            v-model="buzhou"
            placeholder="请选择类型"
            @change="buzhouChange"
          >
            <el-option label="欣赏" :value="0"> </el-option>
            <el-option label="认读" :value="1"> </el-option>
            <el-option label="观察" :value="2"> </el-option>
            <el-option label="分析" :value="4"> </el-option>
            <el-option label="笔法" :value="3"> </el-option>
            <el-option label="视频" :value="7"> </el-option>
            <el-option label="对比" :value="5"> </el-option>
            <el-option label="随堂练习" :value="6"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addObserve(0)" :disabled="xzBtn"
            >新增</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="gcList" stripe v-loading="gcLoading" style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-table :data="props.row.children" stripe style="width: 100%">
            <el-table-column
              prop="observePicture"
              header-align="center"
              align="center"
              label="观察图片"
            >
              <template slot-scope="scope">
                <el-image :src="scope.row.observePicture"></el-image>
              </template>
            </el-table-column>
            <el-table-column
              prop="porder"
              header-align="center"
              align="center"
              label="顺序"
            ></el-table-column>
            <el-table-column header-align="center" align="center" label="操作">
              <template slot-scope="scope">
                <el-button
                  type="text"
                  @click="addObservePicture(1, scope.row.id)"
                  >修改</el-button
                >
                <el-button type="text" @click="deleteById2(scope.row.id)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column
        prop="characterFeature"
        header-align="center"
        align="center"
        label="字形特征"
      ></el-table-column>
      <el-table-column
        prop="characterFeatureSrc"
        header-align="center"
        align="center"
        label="字形特征图片"
      >
        <template slot-scope="scope">
          <el-image :src="scope.row.characterFeatureSrc"></el-image></template
      ></el-table-column>
      <el-table-column
        prop="observeTypefacePicture"
        header-align="center"
        align="center"
        label="观察字体图片"
      >
        <template slot-scope="scope">
          <el-image
            :src="scope.row.observeTypefacePicture"
          ></el-image></template
      ></el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="音频"
        prop="strokesPositionVideo"
      >
        <template slot-scope="scope">
          <audio :src="scope.row.strokesPositionVideo" controls="controls">
            您的浏览器不支持音频播放
          </audio>
        </template>
      </el-table-column>
      <el-table-column header-align="center" align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="addObservePicture(0, scope.row.id)"
              >添加图片</el-button
            >
            <el-button style="" type="text" @click="addObserve(1, scope.row.id)"
              >修改</el-button
            >
            <!-- <el-button type="text" @click="deleteById(scope.row.id)"
              >删除</el-button
            > -->
        </template>
      </el-table-column>
    </el-table>
    <!-- <ul class="chanpterList">
      <li
        v-if="observe.characterFeature != ''"
        :model="observe"
        :key="observe.id"
      >
        <p>
          {{ observe.characterFeature }}

          <span class="acts">
            <el-button type="text" @click="addObservePicture(0, observe.id)"
              >添加图片</el-button
            >
            <el-button style="" type="text" @click="addObserve(1, observe.id)"
              >修改</el-button
            >
          </span>
        </p>

        <ul class="chanpterList videoList">
          <li
            v-for="observePicture in observe.children"
            :key="observePicture.id"
          >
            <p>
              <img
                v-if="observePicture.observePicture"
                :src="observePicture.observePicture"
                class="avatar"
              />
              <span class="acts">
                <el-button
                  type="text"
                  @click="addObservePicture(1, observePicture.id)"
                  >修改</el-button
                >
                <el-button type="text" @click="deleteById2(observePicture.id)"
                  >删除</el-button
                >
              </span>
            </p>
          </li>
        </ul>
      </li>
    </ul> -->
    <el-dialog
      :visible.sync="dialogObserveVisible"
      :title="type == 0 ? '添加观察信息' : '修改观察信息'"
    >
      <el-form
        :rules="dataRule"
        ref="observeForm"
        :model="observe"
        label-width="120px"
      >
        <el-form-item label="字形特征" prop="characterFeature">
          <!-- <el-input v-model="observe.characterFeature" /> -->

          <el-select
            v-model="observe.characterFeature"
            placeholder="请选择类型"
          >
            <el-option label="独体字" value="独体字"> </el-option>
            <el-option label="上下结构" value="上下结构"> </el-option>
            <el-option label="上中下结构" value="上中下结构"> </el-option>
            <el-option label="左右结构" value="左右结构"> </el-option>
            <el-option label="左中右结构" value="左中右结构"> </el-option>
            <el-option label="包围结构" value="包围结构"> </el-option>
            <el-option label="半包围结构" value="半包围结构"> </el-option>
            <el-option label="全包围结构" value="全包围结构"> </el-option>
            <el-option label="笔画偏旁" value="笔画偏旁"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="字形特征图片" prop="characterFeatureSrc">
          <el-upload
            class="avatar-uploader"
            :action="'http://121.41.167.195:8000/api/oss/upload'"
            :show-file-list="false"
            :on-success="handleAvatarSuccess1"
          >
            <img
              v-if="observe.characterFeatureSrc"
              :src="observe.characterFeatureSrc"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="观察字体图片" prop="observeTypefacePicture">
          <el-upload
            class="avatar-uploader"
            :action="'http://121.41.167.195:8000/api/oss/upload'"
            :show-file-list="false"
            :on-success="handleAvatarSuccess2"
          >
            <img
              v-if="observe.observeTypefacePicture"
              :src="observe.observeTypefacePicture"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="语音" prop="strokesPositionVideo">
          <el-upload
            class="upload-uploader"
            ref="audioUpload"
            :action="'http://121.41.167.195:8000/api/oss/upload'"
            :on-exceed="handleExceed"
            :on-remove="handleRemove1"
            :on-success="handleAudioSuccess1"
            :before-upload="beforeAudioUpload"
            :show-file-list="false"
          >
            <audio :src="observe.strokesPositionVideo" controls="controls">
              您的浏览器不支持音频播放
            </audio>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogObserveVisible = false">取 消</el-button>
        <el-button
          :disabled="saveObserveBtnDisabled"
          type="primary"
          @click="saveOrUpdate"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <el-dialog
      :visible.sync="dialogObservePictureVisible"
      :title="type == 0 ? '添加观察图片信息' : '修改观察图片信息'"
    >
      <el-form
        :rules="dataRule1"
        ref="observePictureForm"
        :model="observePicture"
        label-width="120px"
      >
        <el-form-item label="图片" prop="observePicture">
          <el-upload
            class="avatar-uploader"
            :action="'http://121.41.167.195:8000/api/oss/upload'"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img
              v-if="observePicture.observePicture"
              :src="observePicture.observePicture"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="顺序" prop="porder">
          <el-input-number
            v-model="observePicture.porder"
            controls-position="right"
            :min="1"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogObservePictureVisible = false"
          >取 消</el-button
        >
        <el-button
          :disabled="saveObservePictureBtnDisabled"
          type="primary"
          @click="saveOrUpdate1"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <div>
      <el-button @click="previous">上一步</el-button>
      <!-- <el-button :disabled="saveBtnDisabled" type="primary" @click="next"
        >完成</el-button
      > -->
    </div>
  </div>
</template>

<script>
import Vue from "vue";
export default {
  data() {
    return {
      buzhou: 2,
      saveBtnDisabled: false, // 保存按钮是否禁用
      observe: {
        // 观察对象
        strokesPositionVideo: "https://bby-0902.oss-cn-beijing.aliyuncs.com/2021/05/12faa7ab167fdb4be59ae469bfcd7d0ffc观察通用音频.mp3",
        observeTypefacePicture: "",
        characterFeature: "",
        characterFeatureSrc: "",
        tid: null,
        id: null,
      },
      observePicture: {
        id: null,
        observePicture: "",
        porder: 1,
        pid: null,
      },
      gcList: [],
      gcLoading: false,
      xzBtn: false,
      type: 0, // 0:添加1:修改
      dialogObserveVisible: false,
      saveObserveBtnDisabled: false,
      dialogObservePictureVisible: false,
      saveObservePictureBtnDisabled: false,
      dataRule: {
        characterFeature: [
          { required: true, message: "字形特征不能为空", trigger: "blur" },
        ],
        characterFeatureSrc: [{ required: true, message: "图片不能为空" }],
        observeTypefacePicture: [{ required: true, message: "图片不能为空" }],
        strokesPositionVideo: [{ required: true, message: "音频不能为空" }],
      },
      dataRule1: {
        porder: [{ required: true, message: "顺序不能为空", trigger: "blur" }],
        observePicture: [{ required: true, message: "图片描述不能为空" }],
      },
    };
  },

  activated() {
    this.buzhou = 2;
    this.observe.tid = this.$route.params.id;
    // 有数据就要回显数据
    this.getObserveByWordId();
  },

  methods: {
    buzhouChange(value) {
      switch (value) {
        case 0:
          // 欣赏
          this.$router.push({
            path: "/ruanbi/ruanbixsAdd/" + this.$route.params.id,
          });
          break;
        case 1:
          // 认读
          this.$router.push({
            path: "/ruanbi/ruanbirdAdd/" + this.$route.params.id,
          });
          break;
        case 2:
          // 观察
          this.$router.push({
            path: "/ruanbi/ruanbigcAdd/" + this.$route.params.id,
          });
          break;
        case 3:
          // 笔法
          this.$router.push({
            path: "/ruanbi/ruanbibfAdd/" + this.$route.params.id,
          });
          break;
        case 4:
          // 分析
          this.$router.push({
            path: "/ruanbi/ruanbifxAdd/" + this.$route.params.id,
          });
          break;
        case 5:
          // 对比
          this.$router.push({
            path: "/ruanbi/ruanbidbAdd/" + this.$route.params.id,
          });
          break;
        case 6:
          // 随堂练习
          this.$router.push({
            path: "/ruanbi/ruanbistlxAdd/" + this.$route.params.id,
          });
          break;
        default:
          // 视频
          this.$router.push({
            path: "/ruanbi/wordVideo/" + this.$route.params.id,
          });
          break;
      }
    },
    beforeAudioUpload(file) {
      const fileName = file.name;
      const fileType = fileName.substring(fileName.lastIndexOf("."));
      if (fileType !== ".mp3") {
        this.$message.error("上传音频只能是mp3!");
        return false;
      }
      return true;
    },
    //上传成功后的回调函数
    handleAvatarSuccess2(response) {
      this.observe.observeTypefacePicture = response.url;
    },
    //上传成功后的回调函数
    handleAvatarSuccess1(response) {
      this.observe.characterFeatureSrc = response.url;
    },
    //上传成功后的回调函数
    handleAvatarSuccess(response) {
      this.observePicture.observePicture = response.url;
    },
    //上传成功后的回调函数
    handleAudioSuccess1(response) {
      this.observe.strokesPositionVideo = response.url;
    },
    //上传成功后的回调函数
    handleAudioSuccess(response) {
      this.observePicture.sounds = response.url;
    },
    // 超出上传限制
    handleExceed(files, fileList) {
      this.$message.warning(`只能上传一个音频`);
    },
    // 删除音频
    handleRemove1(file, fileList) {
      this.observe.strokesPositionVideo = "";
    },
    // 删除音频
    handleRemove(file, fileList) {
      this.observePicture.sounds = "";
    },
    getObserveByWordId() {
      this.gcLoading = true;
      this.$http({
        url: this.$http.adornUrl(
          `/sf/cbobserve/getObserveByWordId/${this.observe.tid}`
        ),
        method: "get",
      }).then(({ data }) => {
        this.gcList = [];
        this.xzBtn = false;
        if (data && data.code === 0) {
          if (data.cbObserve) {
            this.observe = data.cbObserve;
            this.gcList.push(this.observe);
            this.xzBtn = true;
          }
        } else {
          // this.observe = { typefaceName: "", editionCover: "", id: null };
        }
        this.gcLoading = false;
      });
    },
    // 章节对话框中的保存或修改
    saveOrUpdate() {
      if (this.type == 0) {
        // 添加章节
        this.observeFormSubmit();
      } else {
        // 修改章节
        this.updateObserve();
      }
    },
    // 更新章节图片信息
    updateObserve() {
      this.$refs["observeForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbobserve/update`),
            method: "post",
            data: this.$http.adornData(
              {
                id: this.observe.id,
                tid: this.observe.tid,
                characterFeature: this.observe.characterFeature,
                characterFeatureSrc: this.observe.characterFeatureSrc,
                observeTypefacePicture: this.observe.observeTypefacePicture,
                strokesPositionVideo: this.observe.strokesPositionVideo,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "修改成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getObserveByWordId();
                  this.dialogObserveVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 更新章节图片信息
    updateObservePicture() {
      this.$refs["observePictureForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbobservepicture/update`),
            method: "post",
            data: this.$http.adornData(
              {
                id: this.observePicture.id,
                pid: this.observePicture.pid,
                porder: this.observePicture.porder,
                observePicture: this.observePicture.observePicture,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "修改成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getObserveByWordId();
                  this.$refs["audioUpload"].clearFiles();
                  this.dialogObservePictureVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 章节图片对话框中的保存或修改
    saveOrUpdate1() {
      if (this.type == 0) {
        // 添加章节
        this.observePictureFormSubmit();
      } else {
        // 修改章节
        this.updateObservePicture();
      }
    },
    // 打开对话框，添加、修改
    addObserve(status, id) {
      // 重置表单 应该只是把表单有的清空而已
      this.$nextTick(() => {
        this.$refs.observeForm.resetFields();
      });
      if (status == 0) {
        // 添加
        this.type = 0;
        // 清空
        this.observe.id = null;
        this.observe.characterFeatureSrc = "";
      } else {
        // 修改
        this.type = 1;
        // 回显数据
        this.$http({
          url: this.$http.adornUrl("/sf/cbobserve/info/" + id),
          method: "get",
        }).then(({ data }) => {
          this.observe.id = data.cbObserve.id;
          this.observe.characterFeature = data.cbObserve.characterFeature;
          this.observe.tid = data.cbObserve.tid;
          this.observe.characterFeatureSrc = data.cbObserve.characterFeatureSrc;
          this.observe.observeTypefacePicture =
            data.cbObserve.observeTypefacePicture;
          this.observe.strokesPositionVideo =
            data.cbObserve.strokesPositionVideo;
        });
      }
      this.dialogObserveVisible = true;
    },
    // 打开对话框，添加、修改
    addObservePicture(status, id) {
      // 重置表单 应该只是把表单有的清空而已
      this.$nextTick(() => {
        this.$refs.observePictureForm.resetFields();
      });
      if (status == 0) {
        // 添加
        this.type = 0;
        // 清空
        this.observePicture.id = null;
        this.observePicture.pid = id;
        this.observePicture.observePicture = "";
      } else {
        // 修改
        this.type = 1;
        // 回显数据
        this.$http({
          url: this.$http.adornUrl("/sf/cbobservepicture/info/" + id),
          method: "get",
        }).then(({ data }) => {
          this.observePicture.id = data.cbObservePicture.id;
          this.observePicture.observePicture =
            data.cbObservePicture.observePicture;
          this.observePicture.porder = data.cbObservePicture.porder;
          this.observePicture.pid = data.cbObservePicture.pid;
        });
      }
      this.dialogObservePictureVisible = true;
    },
    // 表单提交
    observeFormSubmit() {
      // 表单验证
      this.$refs["observeForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbobserve/save`),
            method: "post",
            data: this.$http.adornData(
              {
                strokesPositionVideo: this.observe.strokesPositionVideo,
                observeTypefacePicture: this.observe.observeTypefacePicture,
                characterFeatureSrc: this.observe.characterFeatureSrc,
                characterFeature: this.observe.characterFeature,
                tid: this.observe.tid,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "添加成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  // 刷新
                  this.getObserveByWordId();
                  // 只能添加一次
                  this.xzBtn = true;
                  this.dialogObserveVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 表单提交
    observePictureFormSubmit() {
      // 表单验证
      this.$refs["observePictureForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbobservepicture/save`),
            method: "post",
            data: this.$http.adornData(
              {
                pid: this.observePicture.pid,
                porder: this.observePicture.porder,
                observePicture: this.observePicture.observePicture,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "添加成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  // 刷新
                  this.getObserveByWordId();
                  this.$refs["audioUpload"].clearFiles();
                  this.dialogObservePictureVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 通过章节id删除章节
    deleteById(id) {
      this.$confirm(`确定对[欣赏id=${id}]进行[删除]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/sf/cbrd/delete"),
            method: "post",
            data: this.$http.adornData([id], false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "删除成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getObserveByWordId();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
    // 通过章节id删除章节
    deleteById2(id) {
      this.$confirm(`确定对[观赏图片id=${id}]进行[删除]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/sf/cbobservepicture/delete"),
            method: "post",
            data: this.$http.adornData([id], false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "删除成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getObserveByWordId();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
    // 上一步
    previous() {
      this.$router.push({
        path: "/ruanbi/word7",
      });
    },
    next() {
      this.$router.push({
        path: "/ruanbi/ruanbiInfo",
      });
    },
  },
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.chanpterList {
  position: relative;
  list-style: none;
  margin: 0;
  padding: 0;
}
.chanpterList li {
  position: relative;
}
.chanpterList p {
  float: left;
  font-size: 20px;
  margin: 10px 0;
  padding: 10px;
  height: 70px;
  line-height: 50px;
  width: 100%;
  border: 1px solid #ddd;
}
.chanpterList .acts {
  float: right;
  font-size: 14px;
}

.videoList {
  padding-left: 50px;
}
.videoList p {
  float: left;
  font-size: 14px;
  margin: 10px 0;
  padding: 10px;
  height: 220px;
  line-height: 30px;
  width: 100%;
  border: 1px dotted #ddd;
}
</style>
